var util = new NT.utilObj.util();
var loginUserName;
var orgCode;
var orgPath;
var orgName;
var orgId;
/*
 * 用户列表显示
 */
var queryGridUrl = "../loan/findByNeedApprove.action";

var addDataUrl = "../loanApp/addApplication.action";
var editDataUrl = "../firm/updateFirm.action";
var removeDataUrl = "../firm/delFirm.action";

/*
 *查询用户资料 
 */
var querDetialUrl = "../loan/findById.action";

var nowOperate;
/**
 * 加载数据
 */
var loadData = function(){
	var headParam = [];
	//headParam.push("id");
	headParam.push("loanId");
	headParam.push("initTime");
	//业务种类
	headParam.push("loanType");
	//申贷金额
	headParam.push("loanAmount");
	//贷款期限
	headParam.push("loanTerm");
	//贷款人
	headParam.push("borrowerName");
	//客户经理
	headParam.push("operator");
	//进件处理人
	headParam.push("submiteHandler");
	//审查处理人
	headParam.push("approvalHandler");
	var url = queryGridUrl;
	
	var defaultBtns = {"viewBtn":"show","editBtn":"hidden","removeBtn":"hidden"};
	var operateBtns = [];
	var historyBtn = {'btnName':'historyBtn','text':'<button class="btn btn-sm btn-info" onclick="showHistory(this)" title="查看历史">' +
			' <i class="glyphicon glyphicon-inbox"></i>' +
			'</button>'};
	
	operateBtns.push(historyBtn);
	
	var querParam = getQueryGridParam();
	if(querParam!=null)
	{
		var gridObj = {};
		gridObj["url"] = url;
		gridObj["headParam"] = headParam;
		gridObj["queryParam"] = querParam;
		gridObj["defaultBtns"] = defaultBtns;
		gridObj["operateBtns"] = operateBtns;
		gridObj["pk"] = "loanId";
		//如果需要获取列表中其余字段值
		gridObj["columnValue"] = "loanId";
		gridObj["page"] = true;
		//gridObj["checked"]=false;
		gridObj["xuhao"]=true;
		
		var nTGridBean = new NTGridBean();
		nTGridBean.init(gridObj);
		nTGridBean.loadGrid();
	}
};


/**
 * 获得pk
 */
var getPk = function(btn){
	var pk = $(btn).parent().parent().parent().attr("pk");
	return pk;
};
/**
 * 获得loanId
 */
var getColumn = function(btn){
	var columnValue = $(btn).parent().parent().parent().attr("columnValue");
	return columnValue;
};


/**
 * 详情
 */
var viewData = function(param,viewSuccessFun){
	operateUtil.viewData(querDetialUrl,param,viewSuccessFun);
};


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){
	window.checkForm.openFun().init({path:webPath,form:'addOrEditForm'}); 
	$("#addFirmDate").val(GetDateStr(-30));
	$("#addFirmDate").datetimepicker({
		format: "yyyy-mm-dd",
		autoclose:true,
		minView:2,
		language:'cn'
	});
	$("#addOrEditSaveBtn").click(function(){
		var returnValue=window.checkForm.openFun().onSubmit();
		if(nowOperate=="add"&&returnValue ){
			var param = getAddParam();
			if(param!=undefined){
				addData(param);
			}
		}else if(nowOperate=="edit"&&returnValue ){
			var param = getEditParam();
			if(param!=undefined){
				var returnValue=compareStore();
				if(returnValue==0){
					if(param!=null){
						editData(param);
					}
				}else{
					util.sysAlert("您尚未更改任何内容，保存失败！");
				}
			}
		}

	});
	
	$("#queryGridBtn").click(function(){
		loadData();
	});
	$("#upload").click(function(){
		$("#upload_form").submit();
	});
	loadData();	
	loadLoginUserInfo();
	$('input:radio[name="businessTypes"]').change( function(){
		var item = $("input[name='businessTypes']:checked").val();
		//切换时清除备注内容
		//alert(item);
		$("#addSyfRemark1").val(0);
		$("#addSyfRemark2").val("");
		$("#addXfdkRemark").val(0);
		$("#addJydkRemark1").val(0);
		$("#addJydkRemark2").val("");
		$("#addNhxexydkRemark").val(0);
		$("#addEsfajRemark").val(0);
		if(item=='1')
		{
			$("#syfRe").show();
			$("#xfkdRe").hide();
			$("#jydkRe").hide();
			$("#nhxexydkRe").hide();
			$("#esfajRe").hide();
		}
		else if(item=='2')
		{
			$("#syfRe").hide();
			$("#xfkdRe").show();
			$("#jydkRe").hide();
			$("#nhxexydkRe").hide();
			$("#esfajRe").hide();
		
		}else if(item=='3')
		{
			$("#syfRe").hide();
			$("#xfkdRe").hide();
			$("#jydkRe").show();
			$("#nhxexydkRe").hide();
			$("#esfajRe").hide();
		
		}else if(item=='4')
		{
			$("#syfRe").hide();
			$("#xfkdRe").hide();
			$("#jydkRe").hide();
			$("#nhxexydkRe").show();
			$("#esfajRe").hide();
		
		}else if(item=='5')
		{
			$("#syfRe").hide();
			$("#xfkdRe").hide();
			$("#jydkRe").hide();
			$("#nhxexydkRe").hide();
			$("#esfajRe").show();
		
		}
	});
});

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function loadLoginUserInfo() {
	var util = new NT.utilObj.util();
	util.emmAjax({
		url : '../login/getLoginUserInfo.action',
		success : function(data) {
			var obj = eval('(' + data + ')');
			loginUserName = obj.data.userName;
			orgCode = obj.data.orgCode;
			orgPath = obj.data.orgPath;
			orgName = obj.data.orgName;
			orgId = obj.data.orgId;
		}
	});
};


/**
 * 详情
 */
var viewBtn = function(btn){
	var id = getPk(btn);
	var loanId = getColumn(btn)
	var url = "../loanAppDetails/detailsLoanApp.html?id=" + id+"&loanId="+loanId;
	window.location.href = encodeURI(url);
};

/**
 * 批复
 */
var showPf = function(btn){
	var id = getPk(btn);
	var loanId = getColumn(btn)
	var url = "./detailsLoanApp2Examine.html?id=" + id+"&loanId="+loanId+"&loginUserName="+loginUserName;
	window.location.href = encodeURI(url);
};

/**
 * 查看历史
 */
var showHistory = function(btn){
	var id = getPk(btn);
	var loanId = getPk(btn)
	var url = "../loanAppHistory/detailsLoanAppHistory.html?loanId="+loanId+"&loginUserName="+loginUserName;
	window.location.href = encodeURI(url);
};

/**
 * 获得查询列表参数
 */
var getQueryGridParam = function(){
	var QNum = $("#query_loanId").val();
	var QType =$("#query_bussniessType").val()=="0"?"":$("#query_bussniessType").val();
	var QName = $("#query_borrowName").val();
	var startTime=$("#statisticStartTime").val();
	//var endTime=getFormatDateStr($("#statisticEndTime").val(),1);
	var endTime=$("#statisticEndTime").val();
	if(startTime!=""&&endTime=="")
	{
		util.sysTips("开始时间和结束时间必须同时输入","statisticEndTime"); return null;
	}
	else if(startTime==""&&endTime!="")
	{
		util.sysTips("开始时间和结束时间必须同时输入","statisticStartTime"); return null;
	}
	else if(startTime!=""&&endTime!=""&&endTime<=startTime)
	{util.sysTips("结束时间必须大于开始时间","statisticEndTime"); return null;}
	else{
		var param = {
				'loanId' : QNum,
				'loanType' : QType,
				'borrowerName' : QName,
				'queryStartTime' : startTime,
				'queryEndTime' : endTime
				//'operator' : loginUserName//,
				//'curProcessName':'提交申请'
			};
		return param;
	}
};

$("#statisticStartTime").datetimepicker({
	format: "yyyy-mm-dd",
	autoclose:true,
	startView: 2,
	minView: 2,
	language:'cn'
});
$("#statisticEndTime").datetimepicker({
	format: "yyyy-mm-dd",
	autoclose:true,
	startView: 2,
	minView: 2,
	language:'cn'
});

/**
 * 获得编辑的参数
 * @return
 */
var getEditParam = function(){
	var addFirmNum = $("#addFirmNum").val();
	var addFirmName = $("#addFirmName").val();
	var addContact = $("#addContact").val();
    var addTelephone = $("#addTelephone").val();
    var addMobile = $("#addMobile").val();
    var addFax = $("#addFax").val();
	var addEmail = $("#addEmail").val();
	var addAddress = $("#addAddress").val();
	var addFirmDate = $("#addFirmDate").val();
	var addRemark = $("#addRemark").val();
	var param='{'
	param=param+('\"firmNum\":\"'+addFirmNum+ '\",');
	param=param+('\"firmName\":\"'+addFirmName+ '\",');
	param=param+('\"contact\":\"'+addContact+ '\",');
	param=param+('\"telephone\":\"'+addTelephone+ '\",');
	param=param+('\"mobilePhone\":\"'+addMobile+ '\",');
	param=param+('\"fax\":\"'+addFax+ '\",');
	param=param+('\"email\":\"'+addEmail+ '\",');
	param=param+('\"address\":\"'+addAddress+ '\",');
	param=param+('\"firmDate\":\"'+addFirmDate+ '\",');
	param=param+('\"remark\":\"'+addRemark+ '\"');
	//param = param.substring(0, param.length - 1);
	param += '}';
	param=str2Json(param);
				
	store_new['firmName']=addFirmName;
	store_new['contact']=addContact;
	store_new['telephone']=addTelephone;
	store_new['mobile']=addMobile;
	store_new['email']=addEmail;
	store_new['fax']=addFax;
	store_new['address']=addAddress;
	store_new['firmDate']=addFirmDate;
	store_new['remark']=addRemark;
	return param;
};

//审批
function approve(result){
	
	//var processUser = getUrlParam('loginUserName');
	var params = getSpDataOfNumbersParam();
	if(result=="FAIL")
	{
		//退件
		util.confirmView(function(){
			util.emmAjax({
				url : '../loan/loanApprovalFail.action',
				type : 'post',
				data : params,
				cache : false,
				dataType : 'text',
				success : function(data) {
					var obj =str2Json(data);
					if(obj.success=='true')
					{
						util.sysAlert("标记为失败完成");
						window.location.href = "./loanApp2Examine.html";
					}
					else{
						util.sysAlert(obj.data);
					}
				}
			});
		},"确定标记为失败吗?");
	}else if(result=="SUCCESS")
	{
		//审核
		//var id = getUrlParam('loanId');
		util.confirmView(function(){
			util.emmAjax({
				url : '../loan/loanApprovalSucc.action',
				type : 'post',
				data : params,
				cache : false,
				dataType : 'text',
				success : function(data) {
					var obj =str2Json(data);
					if(obj.success=='true')
					{
						util.sysAlert("标记为成功完成");
						window.location.href = "./loanApp2Examine.html";
					}
					else{
						util.sysAlert(obj.data);
					}
				}
			});
		},"确定标记为成功吗?");
	}
	
};

/**
 * 获得批量审批的参数
 * @return
 */
var getSpDataOfNumbersParam = function(){
	var checkboxs = $("#table").find("tbody").find("input[type='checkbox']");
	var pks = new Array();
	
	$(checkboxs).each(function(index,ele){
		if($(this).is(":checked")){
			var pk = getPk($(this).parent());
			pks.push(pk);
		}
	});
	
	if(pks.length>0){
		
		var param="{";
		//param=param+('\"curProcessUser\":\"'+loginUserName+'\",');
		//param=param+('\"approvalHandler\":\"'+reviewUser+'\",');
		
		for(var i=0;i<pks.length;i++)
			{
				param=param+('\"ids['+i+']\":\"'+pks[i]+'\",');
			}
		param = param.substring(0, param.length - 1);
		param += '}';
		//alert(param);
		param=util.str2Json(param);
		//var param = {"paramNames":pks};
		return param;
	}
	else{
		util.sysTips("请选择需要分配的数据！ ","selectremove");
	}
};

/**
 *将json字符串转化为json对象
 */
function str2Json(jsonStr){
	var json = eval("(" + jsonStr + ")"); 
	return json;
};
